import { Component, OnInit } from '@angular/core';
import { NavController, PickerController } from '@ionic/angular';
import { ToolService } from 'src/app/service/tool.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { HttpService } from 'src/app/service/http.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { Storage } from '@ionic/storage';

@Component({
  selector: 'app-rule',
  templateUrl: './rule.page.html',
  styleUrls: ['./rule.page.scss'],
})
export class RulePage implements OnInit {
  public ruleInfo={"content":"","notes_title":"","statement":""};
  constructor(private navCtrl: NavController,private http:HttpService, private popWin:PopwinService,private pickercontroller:PickerController,private store:Storage) { }

  ngOnInit() {
    this.ruleDate();
  }

  backClick() {
    this.navCtrl.navigateBack("tabs/my");
  }

  ruleDate(toplotcode: string = "pks") {
    let that = this;
    let obj = "{\"toplotcode\":\"" + toplotcode + "\"}";
    this.http.get(UrlService.USER_GAMRULE, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
       let res=  ToolService.aesDecodeToJson(data.data);
       that.ruleInfo=res.artinfo;
       console.log(res);
        //成功的逻辑
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }

  async  openPicker(numColumns = 1) {
    let that=this;
    var tempData;
    await this.store.get(ConfigService.ALL_LOT_STORE_KEY).then((data)=>{
        tempData=data;
    });
    if(ToolService.isEmpty(tempData)){
       return;
    } 
   let data= tempData;
    if(data.length<=0){
      return;
    }
   let numOptions=data.length;
   let multiColumnOptions=[];
   let arr=[];
    for(let tempItem in data){
      arr.push(data[tempItem].name);
    }
    multiColumnOptions.push(arr);
    const picker = await this.pickercontroller.create({
      columns: this.getColumns(numColumns, numOptions, multiColumnOptions),
      buttons: [
        {
          text: '取消',
          role: 'cancel'
        },
        {
          text: '确认',
          handler: (value) => {
            that.ruleDate(data[value['col-0'].value].key);
          }
        }
      ]
    });
    await picker.present();
  }

  getColumns(numColumns, numOptions, columnOptions) {
    let columns = [];
    for (let i = 0; i < numColumns; i++) {
      columns.push({
        name: `col-${i}`,
        options: this.getColumnOptions(i, numOptions, columnOptions)
      });
    }
    return columns;
  }

  getColumnOptions(columnIndex, numOptions, columnOptions) {
    let options = [];
    for (let i = 0; i < numOptions; i++) {
      options.push({
        text: columnOptions[columnIndex][i % numOptions],
        value: i
      })
    }
    return options;
  }










}
